<template>
  <el-card class="box-card">
    <div class="header">
      <el-input
        v-model="pageQuery.id"
        placeholder="字典名称"
        size="small"
        style="width: 300px;"
        class="filter-item"
        @keyup.enter.native="submitQuery"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        size="small"
        icon="el-icon-search"
        @click="submitQuery"
      >搜索
      </el-button>
      <el-button
        class="filter-item"
        size="small"
        icon="el-icon-delete"
        type="default"
        style="margin-left:0;"
        @click="clearQuery"
      >清空
      </el-button>
      <el-button
        v-waves
        class="filter-item"
        type="success"
        icon="el-icon-plus"
        @click="showCreateDialog"
      >新增字典
      </el-button>
    </div>
    <!-- 头部end -->
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" min-width="80px" align="center">
        <template slot-scope="{row}">
          <span class="link-type">{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="字典名称" min-width="100px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.dictName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="字典标识" min-width="100px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.dictCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" min-width="90px" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.state ? 'primary':'danger' ">{{ scope.row.state ? '正常' : '封存' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="380px"
      >
        <template slot-scope="{row}">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="showUpdateDialog(row.id)"
          >编辑
          </el-button>
          <el-button
            type="default"
            icon="el-icon-edit"
            size="mini"
            @click="handleDrawerOpen(row.dictName,row.id,row.dictCode)"
          >子项
          </el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="deleteHandle(row.id)"
            plain
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="titleMap[dialogStatus]" :lock-scroll="true" :before-close="handleClose"
               :visible.sync="dialogFormVisible">
      <div class="dialogBody">
        <el-scrollbar wrap-class="scrollbar-wrapper">
          <el-form
            ref="form"
            :model="form"
            :rules="rules"
            label-width="80px"
            :disabled="!(dialogStatus ==='update' || dialogStatus ==='create')"
          >
            <el-row>
              <el-col :xs="24" :md="24">
                <el-form-item label="字典名称" prop="dictName">
                  <el-input
                    v-model="form.dictName"
                    placeholder="请输入字典名称"
                  />
                </el-form-item>
              </el-col>
              <el-col :xs="24" :md="24">
                <el-form-item label="字典标识" prop="dictCode">
                  <el-input
                    v-model="form.dictCode"
                    placeholder="请输入字典标识"
                    :disabled="!(dialogStatus ==='create')"
                  />
                </el-form-item>
              </el-col>
              <el-col :xs="24" :md="24">
                <el-form-item label="字典值" prop="dictValue">
                  <el-input
                    v-model="form.dictValue"
                    placeholder="请输入字典值"
                  />
                </el-form-item>
              </el-col>
              <el-col v-show="dialogChildStatus" :xs="24" :md="24">
                <el-form-item label="父级名称">
                  <el-input
                    v-model="childDrawer.title"
                    :disabled="true"
                    class="filter-item"
                  />
                </el-form-item>
              </el-col>
              <el-col :xs="24" :md="24">
                <el-form-item label="状态" prop="state">
                  <el-select
                    v-model="form.state"
                    placeholder="状态"
                    clearable
                    style="width: 150px"
                    class="filter-item"
                  >
                    <el-option
                      v-for="item in statusOptions"
                      :key="item.key"
                      :label="item.value"
                      :value="item.key"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="24" :md="24">
                <el-form-item label="排序字段" prop="sortId">
                  <el-input-number v-model="form.sortId" :min="1" value="0" :max="100" label="排序字段"/>
                </el-form-item>
              </el-col>
              <el-col :xs="24" :md="24">
                <el-form-item label="备注" prop="remarks">
                  <el-input
                    v-model="form.remarks"
                    placeholder="请输入字典备注"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-scrollbar>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button
          v-if="(dialogStatus ==='update' || dialogStatus ==='create')"
          type="primary"
          @click="saveHandle"
        >保存
        </el-button>
      </div>
    </el-dialog>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pageQuery.current"
      :limit.sync="pageQuery.size"
      @pagination="getPage"
    />

    <el-drawer
      size="60%"
      :title="childDrawer.title"
      :visible.sync="childDrawer.drawer"
      :direction="childDrawer.direction"
      :before-close="handleDrawerClose">
      <div class="childTable">
        <div class="header">
          <el-button
            v-waves
            class="filter-item"
            type="success"
            icon="el-icon-plus"
            @click="showChildCreateDialog"
          >新增
          </el-button>
        </div>
        <div class="childTableBody">
          <el-scrollbar wrap-class="scrollbar-wrapper">
            <el-table
              v-loading="listLoading"
              :data="childList"
              border
              fit
              highlight-current-row
              style="width: 100%;"
            >
              <el-table-column label="#" min-width="80px" align="center">
                <template slot-scope="{row}">
                  <span class="link-type">{{ row.id }}</span>
                </template>
              </el-table-column>
              <el-table-column label="字典名称" min-width="100px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.dictName }}</span>
                </template>
              </el-table-column>
              <el-table-column label="字典标识" min-width="100px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.dictCode }}</span>
                </template>
              </el-table-column>
              <el-table-column label="字典值" min-width="100px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.dictValue }}</span>
                </template>
              </el-table-column>
              <el-table-column label="状态" min-width="90px" align="center">
                <template slot-scope="scope">
                  <el-tag :type="scope.row.state ? 'primary':'danger' ">{{ scope.row.state ? '正常' : '封存' }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column
                label="操作"
                align="center"
                width="200px"
              >
                <template slot-scope="{row}">
                  <el-button
                    type="primary"
                    icon="el-icon-edit"
                    size="mini"
                    @click="showChildUpdateDialog(row.id)"
                  >编辑
                  </el-button>
                  <el-button
                    type="danger"
                    icon="el-icon-delete"
                    size="mini"
                    @click="deleteHandle(row.id)"
                  >删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-scrollbar>
        </div>
      </div>
    </el-drawer>
  </el-card>
</template>
<script>
import {page, findById, list, save, deleteById, updateById} from '@/api/dict'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
const statusOptions = [
  {key: true, value: '正常'},
  {key: false, value: '禁用'}
]
export default {
  name: 'SysDict',
  components: {Pagination},
  directives: {waves},
  data() {
    return {
      tableKey: 0,
      list: null,
      childList: null,
      total: 0,
      statusOptions,
      listLoading: true,
      pageQuery: {
        current: 1,
        size: 10,
        id: undefined
      },
      childDrawer: {
        title: undefined,
        direction: 'rtl',
        drawer: false,
        parentCode: undefined
      },
      childListQuery: {
        parentId: undefined
      },
      rules: {
        dictCode: [{required: true, trigger: 'blur', message: '字典标识不能为空'}],
        dictName: [{required: true, trigger: 'blur', message: '字典名称不能为空'}],
        dictValue: [{required: true, trigger: 'blur', message: '字典值不能为空'}],
        parentId: [{required: true, trigger: 'blur', message: '上级ID不能为空'}],
        state: [{required: true, trigger: 'blur', message: '状态不能为空'}]
      },
      dialogFormVisible: false,
      downloadLoading: false,
      titleMap: {
        update: '编辑',
        create: '新增',
        view: '查看'
      },
      dialogStatus: '',
      dialogChildStatus: false,
      form: {}
    }
  },
  created() {
    this.getPage()
  },
  methods: {
    getPage() {
      this.listLoading = true
      page(this.pageQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    getChildList() {
      this.listLoading = true
      list(this.childListQuery).then(response => {
        this.childList = response.data
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleDrawerClose(done) {
      const that = this
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
          that.dialogChildStatus = false
        })
        .catch(_ => {
        })
    },
    handleDrawerOpen(dictName, dictId, code) {
      const that = this
      that.childDrawer.title = dictName
      that.childDrawer.drawer = true
      that.childDrawer.parentCode = code
      that.childListQuery.parentId = dictId
      that.dialogChildStatus = true
      that.getChildList()
    },
    submitQuery() {
      this.pageQuery.current = 1
      this.getPage()
    },
    clearQuery() {
      this.pageQuery.id = undefined
      this.submitQuery()
    },
    showCreateDialog() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.form = {}
    },
    showChildCreateDialog() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.form = {}
      this.form.dictCode = this.childDrawer.parentCode
    },
    showChildUpdateDialog(id) {
      this.resetForm()
      this.dialogStatus = 'update'
      findById(id).then(response => {
        this.form = response.data
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
      this.dialogFormVisible = true
    },
    showUpdateDialog(id) {
      this.resetForm()
      this.dialogStatus = 'update'
      findById(id).then(response => {
        this.form = response.data
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
      this.dialogFormVisible = true
    },
    deleteHandle(id) {
      const me = this
      this.$confirm('是否删除这条数据？?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById(id).then(() => {
          if (this.dialogChildStatus) {
            me.getChildList()
          } else {
            me.getPage()
          }
        })
      })
    },
    saveHandle() {
      const me = this
      me.$refs.form.validate(valid => {
        if (valid) {
          this.downloadLoading = true
          if (this.dialogChildStatus) {
            this.form.parentId = this.childListQuery.parentId
          }
          if (me.dialogStatus === 'create') {
            save(this.form).then(() => {
              this.listLoading = false
              me.dialogFormVisible = false
              if (this.dialogChildStatus) {
                me.getChildList()
              } else {
                me.getPage()
              }
            })
          } else {
            updateById(this.form).then(() => {
              this.listLoading = false
              me.dialogFormVisible = false
              if (this.dialogChildStatus) {
                me.getChildList()
              } else {
                me.getPage()
              }
            })
          }
        } else {
          return false
        }
      })
    },
    resetForm() {
      if (this.$refs.form !== undefined) {
        this.$refs.form.resetFields()
      }
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {
        });
    },
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  height: 100%;
  margin: 10px 8px;
  padding: 0;
}

.childTable {
  padding: 10px 15px 10px 15px;
}

.childTableBody {
  height: 600px;
  overflow: hidden;
}

.header {
  margin-bottom: 10px;
}

.dialogBody {
  height: 400px;
  overflow: hidden;
}

.scrollbar-wrapper {
  overflow-x: hidden !important;
}

.el-scrollbar__bar.is-vertical {
  right: 0px;
}

.el-scrollbar {
  height: 100%;
}
</style>
